package com.westcom.vulb.logic;

import java.util.HashMap;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.westcom.vulb.protocal.http.Request;

//TODO only for test
public class SessionDaemon {
    private static SessionDaemon instance = new SessionDaemon();
    private SessionDaemon() {
    }
    public static SessionDaemon getInstance() {
        return instance;
    }
    
    private ScheduledThreadPoolExecutor mExecutor = new ScheduledThreadPoolExecutor(1);
    public void start() {
        (new Thread(new Runnable() {
            @Override
            public void run() {
                HashMap<String, Object> param = new HashMap<String, Object>();
                param.put("mailAddress", "guoxun_mailbox@163.com");
                param.put("password", "e10adc3949ba59abbe56e057f20f883e");
                HashMap<String, Object> data = BasicLogic.request(Request.URL_LOGIN, param);
                
                String sessionId = (String) data.get("sessionId");
                SessionManager.getInstance().setSessionId(sessionId);
                
                mExecutor.scheduleAtFixedRate(new Runnable() {
                    @Override
                    public void run() {
                        BasicLogic.request(Request.URL_HEART_BEAT, new HashMap<String, Object>());
                    }
                }, 50, 50, TimeUnit.SECONDS);
            }
        })).start();
    }
    
    public void stop() {
        mExecutor.shutdown();
    }
}